<template>
	<view class="page">
		自动登录中
	</view>
</template>

<script>
	import {
		logins
	} from '@/utils/api.js'
	import queryParams from 'uview-ui/libs/function/queryParams';
	export default {
		data() {
			return {
				query: {}
			};
		},
		onLoad(e) {
			this.info()
		},
		methods: {
			info() {
				this.getQuery()
				this.login()
			},
			parseQueryString(queryString) {
				const obj = {};
				const pairs = decodeURIComponent(queryString).split(',');

				pairs.forEach(pair => {
					const [key, value] = pair.split('=');
					obj[key] = isNaN(value) ? value : Number(value);
				});
				
				return obj;
			},
			getQuery() {
				const stringArr = window.location.search.slice(1).split('&')
				this.query = {}
				for (let v of stringArr) {
					const arr = v.split('=')
					this.query[arr[0]] = arr[1]
				}
			},
			async login() {
				const {
					code,
					state
				} = this.query
				const obj = this.parseQueryString(state);
				console.log(obj)
				const data = {
					code,
					appId: obj.appId
				}
				const res = await logins(data)
				uni.setStorageSync('token', res.data.token)
				uni.setStorageSync('appId', obj.appId)
				uni.reLaunch({
					url: obj.path || '/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss">

</style>